/**
 * 
 */
package com.idea.auction.config;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import sun.misc.Cache;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.idea.auction.interceptor.SysLogInterceptor;
import com.idea.auction.shiro.ext.ShiroInterceptor;
import com.idea.auction.shiro.ext.ShiroPlugin;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.Controller;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.ExceptionInterceptor;
import com.jfinal.ext.interceptor.InjectInterceptor;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.tablebind.AutoTableBindPlugin;
import com.jfinal.ext.plugin.tablebind.SimpleNameStyles;
import com.jfinal.ext.route.AutoBindRoutes;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;


/**
 * @author y.z
 */
public class AppConfig extends JFinalConfig {

	public static Long startDate = new Date().getTime();
	private Routes routes;
	/* 
	 * 基本配置
	 */
	@Override
	public void configConstant(Constants me) {
		me.setEncoding("utf-8");
		// 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
		//Properties props = loadProp("application_pro.properties", "application.properties");
		PropKit.use("application.properties");
		//设置开发模式
        me.setDevMode(PropKit.getBoolean("devMode", false));
        me.setViewType(ViewType.FREE_MARKER);
        me.setError404View("/pages/404.html");
		
	}

	/* (non-Javadoc)
	 * @see com.jfinal.config.JFinalConfig#configHandler(com.jfinal.config.Handlers)
	 */
	@Override
	public void configHandler(Handlers me) {
		//me.add(new ResourceHandler());
		DruidStatViewHandler dvh =  new DruidStatViewHandler("/druid",
				new IDruidStatViewAuth() {
					
					@Override
					public boolean isPermitted(HttpServletRequest request) {
						
						return request.getSession().getAttribute("admin") != null;
					}
				});
		me.add(dvh);
		me.add(new ContextPathHandler("ctx"));
		
	}

	/* (non-Javadoc)
	 * @see com.jfinal.config.JFinalConfig#configInterceptor(com.jfinal.config.Interceptors)
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub

		
		me.add(new ShiroInterceptor());
		me.add(new ExceptionInterceptor());
		//me.add(new AdminInterceptor());
		me.add(new SessionInViewInterceptor());
		me.add(new InjectInterceptor());
		me.add(new SysLogInterceptor());
		me.addGlobalServiceInterceptor(new com.jfinal.ext.interceptor.ServiceErrorInterceptor());
		
	}

	/* (non-Javadoc)
	 * @see com.jfinal.config.JFinalConfig#configPlugin(com.jfinal.config.Plugins)
	 */
	@Override
	public void configPlugin(Plugins me) {
		/* 启用数据库连接 */
		//createC3p0Plugin(me);
		createDruid(me);
		//new ServicePlugin().start();
		//me.add(new SpringPlugin());
		
		//配置缓存插件  
        me.add(new EhCachePlugin());
        
        RedisPlugin userRedis = new RedisPlugin("dicCache", "120.26.127.134","123456");
        me.add(userRedis);
        
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
	    /*shiroPlugin.setLoginUrl("/");//登陆url：未验证成功跳转
	    shiroPlugin.setSuccessUrl("/toIndex");//登陆成功url：验证成功自动跳转
	    shiroPlugin.setUnauthorizedUrl("/");//授权url：未授权成功自动跳转
*/	    me.add(shiroPlugin);
		
	}
	
	/**
	 * C3p0Plugin配置
	 */
	private void createC3p0Plugin(Plugins me) {
		C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
				PropKit.get("password"));
		me.add(c3p0Plugin);

		EhCachePlugin ecp = new EhCachePlugin();
		me.add(ecp);
		
		//开启注解
		AutoTableBindPlugin atbp = new AutoTableBindPlugin(c3p0Plugin, SimpleNameStyles.LOWER);
		atbp.setShowSql(true);
		me.add(atbp);
	}
	
	public void createDruid(Plugins me){
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
				PropKit.get("password"));
		dp.addFilter(new StatFilter());
		 WallFilter wall = new WallFilter();
		 wall.setDbType("mysql");
		 dp.addFilter(wall);
		 me.add(dp);
	
		  // ActiveRecordPlugin
		 AutoTableBindPlugin arp = new AutoTableBindPlugin(dp, SimpleNameStyles.LOWER);
		 arp.setShowSql(true);
		 me.add(arp);
	}

	/* (non-Javadoc)
	 * @see com.jfinal.config.JFinalConfig#configRoute(com.jfinal.config.Routes)
	 */
	@Override
	public void configRoute(Routes me) {
		this.routes = me;
		/* 自动扫描 [注解方式] */
		//searchService();
		AutoBindRoutes autoBindRoutes = new AutoBindRoutes();
		List<Class<? extends Controller>> excludeClasses = new ArrayList<Class<? extends Controller>>();
		//excludeClasses.add(IndexController.class);
//		excludeClasses.add(WeixinMsgController.class);
		autoBindRoutes.addExcludeClasses(excludeClasses);
		me.add(autoBindRoutes);
		
	}

}
